package model;

import java.util.*;
import model.semantics.Operation;
import util.reflection.Reflection;
import util.resources.*;

public class Syntax
{
    private static Map<String, String[]> OPERATORS;
    public static final String RIGHT_ASSOCIATIVE = "RIGHT";
    public static final String LEFT_ASSOCIATIVE = "LEFT";
    
    public static final int OPERAND = 0;
    public static final int OPERATOR = 1;
    public static final int RIGHT_PARENTHESES = 2;
    public static final int LEFT_PARENTHESES = 3;
    public static final int COMMA = 4;
    public static final int INFINARY = 5;
    public static final int COLOR = 6;    
    

    public Syntax ()
    {
        ResourceManager opResources = ResourceManager.getInstance();
        opResources.addResourcesFromFile("OperatorsAndFunctions");
        
        OPERATORS = new HashMap<String, String[]>();
        
        for (String key : opResources)
            OPERATORS.put(key, opResources.getStringArray(key, ";"));
    }
    
    static int processToken(String token)
    {
    	if 		(isOperator(token)) 	return OPERATOR;
    	else if (isRightParen(token)) 	return RIGHT_PARENTHESES;
    	else if (isLeftParen(token))	return LEFT_PARENTHESES;
    	else if (isComma(token))		return COMMA;
    	else if (isInfinary(token))		return INFINARY;
    	else if (isColor(token))		return COLOR;
    	else							return OPERAND;
    }
   
    static boolean isRightParen(String token)
    {
    	return token.equals(")");
    }
    
    static boolean isLeftParen(String token)
    {
    	return token.equals("(");
    }
    
    static boolean isComma(String token)
    {
    	return token.equals(",");
    }
    
    static boolean isInfinary(String token)
    {
    	return OPERATORS.get(token)[3].equals("INFINARY");
    }
    
    static boolean isAssociative(String token, String type) 
    {
        if (OPERATORS.get(token)[1].equals(type)) 
        {
            return true;
        }
        return false;
    }
    
	static boolean isOperator(String token)
	{
		return OPERATORS.containsKey(token);
	}
	
	static boolean isColor(String token)
	{
		return token.equals("[");
	}
	
	static int comparePrecedence(String token1, String token2)
	{
		return (Integer.parseInt(OPERATORS.get(token1)[2]) - Integer.parseInt(OPERATORS.get(token2)[2]));
	}
	
}
